#include "..\..\Header\Device\fwWin32Context.h"
#include "..\..\Header\Device\fwAndroidContext.h"
#include "..\..\Header\Device\fwDevice.h"
#include "..\..\Header\Game\fwGlobal.h"

PlatformContext * Device::m_pPlatformContext = NULL;
Device * Device::m_pInstance = NULL;

Device::Device()
{
	LOGI("Device created: \n");
}

Device::~Device()
{
	LOGI("Device destroy: ");
}

Device * Device::GetInstance()
{
	if (!m_pInstance)
		m_pInstance = new Device();
	return m_pInstance;
}

void Device::DestroyInstance()
{
	SAFE_DEL(m_pInstance);
}

void Device::CreateDevicePlatform(DeviceID id, int iWidth, int iHeight, char* szTitle, int iFlags)
{
	GameGlobal::iWidth = iWidth;
	GameGlobal::iHeight = iHeight;

	switch (id)
	{
	case Device::Win32:
#if CURRENT_PLATFORM == PLATFORM_WIN32
		m_pPlatformContext = new Win32Context(GameGlobal::iWidth, GameGlobal::iHeight, szTitle, iFlags);
#endif
		break;
	case Device::Android:
#if CURRENT_PLATFORM == PLATFORM_ANDROID
		m_pPlatformContext = new AndroidContext(GameGlobal::iWidth, GameGlobal::iHeight, szTitle, iFlags);
#endif
		break;
	default:
		break;
	}
}

void Device::RegisterGameContext(GameContext * pGameContext)
{
	if (!m_pPlatformContext)
		LOGE("There isn't PlatformContext was create!\n");

	if (!pGameContext)
		LOGE("Can't register with GameContext is NULL\n");

	m_pPlatformContext->RegisterGame(pGameContext);
}

int Device::Init()
{
	return m_pPlatformContext->Init();
}

void Device::Run()
{
	if (!m_pPlatformContext)
		LOGE("There isn't PlatformContext was create!\n");

	m_pPlatformContext->Run();
}

void Device::Destroy()
{
	m_pPlatformContext->Destroy();
	SAFE_DEL(m_pPlatformContext);
}